<?php
/**
 * @version 1.5 $Id: helper.php
 * @package Joomla 1.6.x, 1.7.x, 2.5.x
 * @subpackage RS-Simpslide flash image slideshow module.
 * @copyright (C) 2010-2015 RS Web Solutions (http://www.rswebsols.com)
 * @license GNU/GPL
 */

defined( '_JEXEC' ) or die( 'Restricted access' ); // no direct access allowed
 
class modRSSimpslideHelper {
	function rsws_sql_regcase($rsws_string, $rsws_encoding='auto') {
		$rsws_ret = '';
		$rsws_max = mb_strlen($rsws_string, $rsws_encoding);
		for($rsws_i=0; $rsws_i<$rsws_max; $rsws_i++) {
			$rsws_char = mb_substr($rsws_string, $rsws_i, 1, $rsws_encoding);
			$rsws_up = mb_strtoupper($rsws_char, $rsws_encoding);
			$rsws_low = mb_strtolower($rsws_char, $rsws_encoding);
			$rsws_ret .= ($rsws_up!=$rsws_low) ? '['.$rsws_up.$rsws_low.']' : $rsws_char;
	  	}
	  	return $rsws_ret;
	}
	
	function getRSSimpslide($module_id, $params) {
		#################
		$rsws_insertSWFOBJECT = $params->get('rsws_insertSWFOBJECT', 1);
		$rsws_width = $params->get('rsws_width', 600);
		$rsws_height = $params->get('rsws_height', 300);
		$rsws_backgroundColor = $params->get('rsws_backgroundColor', 'ffffff');
		$rsws_backgroundTransparency = $params->get('rsws_backgroundTransparency', 100);
		$rsws_randomSlideshow = ($params->get('rsws_randomSlideshow', '1') == '1' ? 'true' : 'false');
		$rsws_loop = ($params->get('rsws_loop', '1') == '1' ? 'true' : 'false');
		$rsws_imageshow = $params->get('rsws_imageshow', 1);
		
		// Set FTP credentials, if given
		$rsws_module_path = JPATH_BASE.DS.'modules'.DS.'mod_rssimpslide'.DS;
		jimport('joomla.client.helper');
		JClientHelper::setCredentialsFromRequest('ftp');
		$ftp = JClientHelper::getCredentials('ftp');
		
		$rsws_file = $rsws_module_path.'xml'.DS.'rssimpslide_'.$module_id.'.xml';
		
		$rsws_txt = '<?xml version="1.0" encoding="UTF-8"?><slideshow width="'.$rsws_width.'" height="'.$rsws_height.'" startWith="1" backgroundColor="0x'.$rsws_backgroundColor.'" backgroundTransparency="'.$rsws_backgroundTransparency.'" randomSlideshow="'.$rsws_randomSlideshow.'" loop="'.$rsws_loop.'">';
		
		if($rsws_imageshow == '1') {
			$rsws_basic_folder = $params->get('rsws_basic_folder', 'images/stories/slideshow/');
			$rsws_basic_url = $params->get('rsws_basic_url', '');
			$rsws_basic_url = str_replace('&amp;', '&', $rsws_basic_url);
			$rsws_basic_url = str_replace('&', '&amp;', $rsws_basic_url);
			$rsws_basic_url_target = $params->get('rsws_basic_url_target', '_self');
			$rsws_basic_transitionTime = $params->get('rsws_basic_transitionTime', 1);
			$rsws_basic_slideShowTime = $params->get('rsws_basic_slideShowTime', 3);
			
			$rsws_jpgimages = glob("".$rsws_basic_folder.modRSSimpslideHelper::rsws_sql_regcase("*.jpg"));
			$rsws_pngimages = glob("".$rsws_basic_folder.modRSSimpslideHelper::rsws_sql_regcase("*.png"));
			$rsws_gifimages = glob("".$rsws_basic_folder.modRSSimpslideHelper::rsws_sql_regcase("*.gif"));
			
			$rsws_image = $rsws_jpgimages;
			
			$j=0;
			for($i=count($rsws_jpgimages);$i<count($rsws_jpgimages)+count($rsws_pngimages);$i++) {
				$rsws_image[$i]=$rsws_pngimages[$j];
				$j=$j+1;
			}
			
			$j=0;
			for($i=count($rsws_image);$i<count($rsws_jpgimages)+count($rsws_pngimages)+count($rsws_gifimages);$i++) {
				$rsws_image[$i]=$rsws_gifimages[$j];
				$j=$j+1;
			}
			for($i=0;$i<count($rsws_image);$i++) {
				if(file_exists($rsws_image[$i])) {
					$rsws_txt .= '<item><path>'.JURI::root().$rsws_image[$i].'</path><link>'.$rsws_basic_url.'</link><target>'.$rsws_basic_url_target.'</target><transitionTime>'.$rsws_basic_transitionTime.'</transitionTime><slideShowTime>'.$rsws_basic_slideShowTime.'</slideShowTime></item>';
				}
			}
		} else {
			$rsws_adv_images = $params->get('rsws_adv_images', '');
			$rsws_adv_urls = $params->get('rsws_adv_urls', '');
			$rsws_adv_urls_target = $params->get('rsws_adv_urls_target', '');
			$rsws_adv_transitionTime = $params->get('rsws_adv_transitionTime', '');
			$rsws_adv_slideShowTime = $params->get('rsws_adv_slideShowTime', '');
			
			$rsws_all_images = explode(',', $rsws_adv_images);
			$rsws_all_urls = explode(',', $rsws_adv_urls);
			$rsws_all_urls_target = explode(',', $rsws_adv_urls_target);
			$rsws_all_transitionTime = explode(',', $rsws_adv_transitionTime);
			$rsws_all_slideShowTime = explode(',', $rsws_adv_slideShowTime);
			for($i=0;$i<count($rsws_all_images);$i++) {
				$rsws_temp_image = trim($rsws_all_images[$i]);
				$rsws_temp_url = trim($rsws_all_urls[$i]);
				$rsws_temp_target = trim($rsws_all_urls_target[$i]);
				$rsws_temp_transitionTime = trim($rsws_all_transitionTime[$i]);
				$rsws_temp_slideShowTime = trim($rsws_all_slideShowTime[$i]);
				if(file_exists($rsws_temp_image)) {
					$rsws_temp_image = JURI::root().$rsws_temp_image;
					$rsws_temp_url = str_replace('&amp;', '&', $rsws_temp_url);
					$rsws_temp_url = str_replace('&', '&amp;', $rsws_temp_url);
					if(($rsws_temp_target == '_blank') || ($rsws_temp_target == '_self')) {
						$rsws_temp_target = $rsws_temp_target;
					} else {
						$rsws_temp_target = '_self';
					}
					if($rsws_temp_transitionTime) {
						$rsws_temp_transitionTime = $rsws_temp_transitionTime;
					} else {
						$rsws_temp_transitionTime = 1;
					}
					if($rsws_temp_slideShowTime) {
						$rsws_temp_slideShowTime = $rsws_temp_slideShowTime;
					} else {
						$rsws_temp_slideShowTime = 3;
					}
					$rsws_txt .= '<item><path>'.$rsws_temp_image.'</path><link>'.$rsws_temp_url.'</link><target>'.$rsws_temp_target.'</target><transitionTime>'.$rsws_temp_transitionTime.'</transitionTime><slideShowTime>'.$rsws_temp_slideShowTime.'</slideShowTime></item>';
				}
			}
		}
		
		$rsws_txt .= '</slideshow>';
		
		jimport('joomla.filesystem.file');
		//if (JFile::exists($rssn_file)) {
			// Try to make the params file writeable
			//echo $rsws_file;
			if (!$ftp['enabled'] && JPath::isOwner($rsws_file) && !JPath::setPermissions($rsws_file, '0755')) {
				//JError::raiseNotice('SOME_ERROR_CODE', JText::_('Could not make the file writable'));
			}
		
			JFile::write($rsws_file, $rsws_txt);
		
			// Try to make the params file unwriteable
			if (!$ftp['enabled'] && JPath::isOwner($rsws_file) && !JPath::setPermissions($rsws_file, '0555')) {
				//JError::raiseNotice('SOME_ERROR_CODE', JText::_('Could not make the file unwritable'));
			}
		//}
		$rsws_document	=& JFactory::getDocument();
		
		if($rsws_insertSWFOBJECT == '1') {
			$rsws_document->addScript( JURI::root().'modules/mod_rssimpslide/js/swfobject.js');
		}
		
		$rsws_document->addScript( JURI::root().'modules/mod_rssimpslide/js/swfmacmousewheel.js');
		
		$rsws_js_controller = 'var cacheBuster = "?t=" + Date.parse(new Date()); var stageW = "'.$rsws_width.'"; var stageH = "'.$rsws_height.'"; var attributes = {}; attributes.id = \'RSSimpSlideModule_'.$module_id.'\'; attributes.name = attributes.id; var params = {}; params.wmode = "transparent"; params.bgcolor = "#'.$rsws_backgroundColor.'"; var flashvars = {}; flashvars.componentWidth = stageW; flashvars.componentHeight = stageH; flashvars.pathToFiles = ""; flashvars.xmlPath = "'.JURI::root().'modules/mod_rssimpslide/xml/rssimpslide_'.$module_id.'.xml"; swfobject.embedSWF("'.JURI::root().'modules/mod_rssimpslide/swf/preview.swf"+cacheBuster, attributes.id, stageW, stageH, "9.0.124", "'.JURI::root().'modules/mod_rssimpslide/swf/expressInstall.swf", flashvars, params); if(swfmacmousewheel) swfmacmousewheel.registerObject(attributes.id);';
		
		$rsws_document->addScriptDeclaration($rsws_js_controller);
		
		$rsws_output_return = '<div id="RSSimpSlideModule_'.$module_id.'"><p>In order to view this object you need Flash Player 9+ support!</p><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player"/></a><p>Powered by <a href="http://www.rswebsols.com/" target="_blank">RS Web Solutions</a></p></div>';
		
		return $rsws_output_return;
    }
}
?>